#include "mypushbutton.h"
#include<QDebug>
#include<QPropertyanimation>
MyPushButton::MyPushButton(QString normalImage,QString pressImage,int size1,int size2){
    this->normalImagePath = normalImage;
    this->pressImagePath = pressImage;
    QPixmap pix;
    bool ret = pix.load(normalImage);
    if(!ret){
        qDebug()<<"图片加载失败";
        return;
    }
    pix = pix.scaled(pix.width()*size1,pix.height()*size2);
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(QIcon(pix));
    this->setIconSize(QSize(pix.width(),pix.height()));
}

void MyPushButton::zoom1(){
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");//创建动画对象
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);//设置弹跳曲线
    animation->start();
}

void MyPushButton::zoom2(){
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");//创建动画对象
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);//设置弹跳曲线
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent * ev){
    if(this->pressImagePath != ""){
        QPixmap pix;
        bool ret = pix.load(this->pressImagePath);
        if(!ret){
            qDebug()<<"图片加载失败";
            return;
        }
        pix = pix.scaled(pix.width()*2,pix.height()*2);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");//图片去边框
        this->setIcon(QIcon(pix));
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(ev);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent * ev){
    if(this->pressImagePath != ""){
        QPixmap pix;
        bool ret = pix.load(this->normalImagePath);
        if(!ret){
            qDebug()<<"图片加载失败";
            return;
        }
        pix = pix.scaled(pix.width()*2,pix.height()*2);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(QIcon(pix));
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(ev);
}
